import { GameObject } from "../game_object/base.js";
import { Controller } from "../controller/base.js";
import { Player } from "../player/base.js";

export class GameMap extends GameObject{
    constructor(root) {
        super();
        this.root = root;
        this.$canvas = $('<canvas width="1280px" height="720px" tabindex=0></canvas>');
        this.ctx = this.$canvas[0].getContext("2d");
        this.root.$kof.append(this.$canvas);
        this.$canvas.focus();

        this.controller = new Controller(this.$canvas);

        this.$timer = this.root.$timer;
        this.time_left = GameMap.TIME_LEFT; // ms
    }

    start() {

    }

    update_timer() {
        let [a, b] = this.root.players;
        this.time_left -= this.time_delta;
        if (a.status !== 6 && b.status !== 6) this.$timer.text(parseInt((this.time_left + 999) / 1000));


        if (this.time_left < 0) this.time_left = 0;
        if (a.status !== 6 && b.status !== 6 && this.time_left <= 0) {
            a.status = b.status = 6;
            a.hp = b.hp = 0;
            a.cur_frame_cnt = b.cur_frame_cnt = 0;
            a.vx = b.vx = 0;
            a.flush_hp();
            b.flush_hp();
        }
    }


    update() {
        this.update_timer();

        this.render();
    }

    render() {
        this.ctx.clearRect(0, 0, this.$canvas.width(), this.$canvas.height());
        // console.log(this.$canvas.width());

        // this.ctx.fillStyle = "black";
        // this.ctx.fillRect(0, 0, this.$canvas.width(), this.$canvas.height());
    }
    

    destroy() {

    }
}

GameMap.TIME_LEFT = 60000;